;(function($) {
	$.ztree = $.ztree || {};
	$.extend($.ztree, {
		defaultAsync : {
			enable: true,
			otherParam : {},
			dataFilter:function(treeId, parentNode, childNodes){
				var newChildNodes = new Array();
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				//只返回父节点下的子节点
				if(parentNode !=null) {
					 for(var i =0; i < childNodes.length; i++) {
						 if(childNodes[i].pId == parentNode.id) {
							 newChildNodes.push(childNodes[i]);
						 }
					 }
				}else {
					newChildNodes = childNodes;
				}
				// 设置是否展开
				  if (newChildNodes&&treeObj.setting.async.isOpen) {
				      for(var i =0; i < newChildNodes.length; i++) {
				    	  newChildNodes[i].open =treeObj.setting.async.isOpen;
				      }
				    }else if (newChildNodes) {
				    	 for(var i =0; i < newChildNodes.length; i++) {
				    		 newChildNodes[i].open =false;
						      }
				    }
				// 调用filteDataAfterAsync方法
				if(treeObj.setting.async.filterDataAfterAsync&&$.isFunction(treeObj.setting.async.filterDataAfterAsync)) {
					return treeObj.setting.async.filterDataAfterAsync.call(this,treeId, parentNode, newChildNodes);
				}else
					return newChildNodes;
			}
		},
		defaultCallback : {
			beforeAsync : function(treeId, treeNode){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(!$.isEmptyObject(treeObj.setting.async.queryInfo) && $.isPlainObject(treeObj.setting.async.queryInfo)){
					var queryInfo = treeObj.setting.async.queryInfo;
					$.extend(treeObj.setting.async.otherParam,{"queryInfo":$.objToJSONString($.extend({},{"listType":"list"},queryInfo))});
					delete treeObj.setting.async.queryInfo;
				}
				if(treeObj.setting.async.beforeLoad&&$.isFunction(treeObj.setting.async.beforeLoad)) {
					return treeObj.setting.async.beforeLoad.call(this,treeId, parentNode);
				}
					
				return true;
			},
			onClick : function(event, treeId, treeNode, clickFlag){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.afterClick&&$.isFunction(treeObj.setting.callback.afterClick)) {
					 treeObj.setting.callback.afterClick.call(this,event,treeId, treeNode,clickFlag);
				}
			},
			onCheck : function(event, treeId, treeNode){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.afterCheck&&$.isFunction(treeObj.setting.callback.afterCheck)) {
					treeObj.setting.callback.afterCheck.call(this,event,treeId, treeNode);
				}
				
			},
			beforeCheck : function(treeId, treeNode){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.beforeCheckNode&&$.isFunction(treeObj.setting.callback.beforeCheckNode)) {
					treeObj.setting.callback.beforeCheckNode.call(this,treeId, treeNode);
				}
				
			},
			beforeClick : function(treeId, treeNode, clickFlag){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.beforeClickNode&&$.isFunction(treeObj.setting.callback.beforeClickNode)) {
					treeObj.setting.callback.beforeClickNode.call(this,treeId, treeNode, clickFlag);
				}
				
			},
			beforeDblClick : function(treeId, treeNode){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.beforeDblNode&&$.isFunction(treeObj.setting.callback.beforeDblNode)) {
					treeObj.setting.callback.beforeDblNode.call(this,treeId, treeNode);
				}
				
			},
			onAsyncError : function(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.onLoadError&&$.isFunction(treeObj.setting.callback.onLoadError)) {
					treeObj.setting.callback.onLoadError.call(this,event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown);
				}
				
			},
			onAsyncSuccess : function(event, treeId, treeNode, msg){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.afterLoad&&$.isFunction(treeObj.setting.callback.afterLoad)) {
					treeObj.setting.callback.afterLoad.call(this,event, treeId, treeNode, msg);
				}
				
			},
			onDblClick : function(event, treeId, treeNode){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.afterDblClick&&$.isFunction(treeObj.setting.callback.afterDblClick)) {
					treeObj.setting.callback.afterDblClick.call(this,event, treeId, treeNode);
				}
				
			},
			onExpand : function(event, treeId, treeNode){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.afterExpand&&$.isFunction(treeObj.setting.callback.afterExpand)) {
					treeObj.setting.callback.afterExpand.call(this,event, treeId, treeNode);
				}
				
			},
			beforeExpand : function(treeId, treeNode){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				if(treeObj.setting.callback.beforeExpandNode&&$.isFunction(treeObj.setting.callback.beforeExpandNode)) {
					treeObj.setting.callback.beforeExpandNode.call(this,treeId, treeNode);
				}
				
			}
		
		},
		defaultCheck : {enable:true,chkStyle: "checkbox",chkboxType: { "Y": "", "N": "" }
},
		defaultData : {simpleData:{enable:true}},
		defaultEdit : {enable: false},
		defaultView : {},
		defaultSelect : {},
		/**
		 * 打开下拉层
		 */
		openDiv:function (treeId) {	
			var divTree = document.getElementById(treeId);
			var opDiv =  document.getElementById(divTree.id+"Div");
			var jdomObj = $(divTree);
			if (!opDiv) {
				opDiv =  document.createElement("div");
				opDiv.id = divTree.id+"Div";
				document.body.appendChild(opDiv);
				$(opDiv).append("<div id='"+divTree.id+"Tree' class='ztree'></div>");
				$(opDiv).css("left", jdomObj.offset().left + "px");
				$(opDiv).css("top", jdomObj.offset().top + jdomObj.outerHeight() + "px");
				$(opDiv).css("width",jdomObj.outerWidth()+"px");
				opDiv.style.zIndex = '9999';
				opDiv.style.position = 'absolute';
				opDiv.style.background = '#f0f6e4';
				opDiv.style.display = 'block';
				opDiv.style.cursor = 'default';
				opDiv.style.overflow = 'hidden';
				opDiv.style.border = '1px solid #000';
			}
			opDiv.style.display = 'block';
			var treeObj = $.fn.zTree.getZTreeObj(divTree.id+"Tree");
			if(!treeObj) {
				treeObj = $.fn.zTree.init($("#"+divTree.id+"Tree"), eval(divTree.id+"Setting"));
			}
			$(document).bind("mousedown", function(event){ $.ztree.closeDiv(event,treeId);});
		},
		
		/**
		 * 关闭下拉层
		 */
		closeDiv:function (event,treeId) {
			if (!(event.target.id == treeId || event.target.id == (treeId+"Div") || $(
					event.target).parents("#"+treeId+"Div").length > 0)) {
				$("#"+treeId+"Div").fadeOut("fast");
				$("body").unbind("mousedown");
			}
		},
		/**
		 * 初始化下拉层
		 */
		initSelect:function (treeId) {
			var domObj = document.getElementById(treeId);
			if($.browser.msie){
				domObj.attachEvent('onmousedown',function(event){ $.ztree.openDiv(treeId);});
			}else{
				domObj.addEventListener('click',openDiv,true);
			}
		}
		
		
	});
})(jQuery);
